﻿using Intex.Tools;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Linq;

namespace WPFExcelReport
{

    public class YCACOFSData : BaseFunction
    {
        public INTEXDBC INTEXDBC;
        public SETINTEXDB INTEXDB = new SETINTEXDB();
        public DataSet SetCAD;
        public DataSet SetExt
        {
            get
            {
                return Convert2Intex.SetExt;
            }
            set
            {
                Convert2Intex.SetExt = value;
            }
        }
        public Convert2Intex Convert2Intex;
        public YCACOFSData()
            : base()
        {

        }
        public YCACOFSData(OracleConnection _OracleDB, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_OracleDB, _proxy, userID)
        {
            INTEXDBC = new INTEXDBC(_OracleDB, _proxy);
            Convert2Intex = new Convert2Intex();
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
        }
        public YCACOFSData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
            Convert2Intex = new Convert2Intex();
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
        }
        public DataSet Convert2XDECK(DataRow dwData, DataRow dwXDECK)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "ICC_XDECK_MAP";
            DataTable tbMapID = new DataTable();
            tbMapID = SetCAD.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "ICC_XDECK";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim().Trim() == "ICC_XDECK"
                        select dwMap;
            foreach (DataRow dwTemp in query)
            {
                Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp, dwData);
            }
            dwIntex["QTYSTD"] = System.Math.Round(System.Convert.ToDecimal(dwIntex["QTYSTD"].ToString()), 0);
            tbIntex.Rows.Add(dwIntex);
            setReturn.Merge(tbIntex);
            return setReturn;
        }
        public DataSet BuildData(DataRow dwInput)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            try
            {
                string krs = dwInput["KRS"].ToString();
                string lfd = dwInput["LFD"].ToString();

                DataRow dwData = INTEXDB.ABKOPF.NewRow();
                dwData["ABKMANDANT"] = "00";
                dwData["ABKAUFKRS"] = krs;
                dwData["ABKAUFLFD"] = lfd;
                string art = dwInput["ART"].ToString();
                string var = dwInput["VAR"].ToString();
                string color = "";
                string size = "";
                Result = INTEXDBC.QueryABKOPF(dwData);
                Return.Merge(Result);
                #region=======Order======
                foreach (DataRow row in Return.Tables["ABKOPF"].Rows)
                {
                    dwData = INTEXDB.ABPOS.NewRow();
                    dwData["ABPMANDANT"] = row["ABKMANDANT"];
                    dwData["ABPAUFKRS"] = row["ABKAUFKRS"];
                    dwData["ABPAUFLFD"] = row["ABKAUFLFD"];
                    Result = INTEXDBC.QueryABPOS(dwData, 99);
                    Return.Merge(Result);

                    dwData = INTEXDB.ICC_ABAUFM.NewRow();
                    dwData["ABAMANDANT"] = row["ABKMANDANT"];
                    dwData["ABAVKAUFKRS"] = row["ABKAUFKRS"];
                    dwData["ABAVKAUFLFD"] = row["ABKAUFLFD"];
                    Result = INTEXDBC.QueryICC_ABAUFM(dwData, 99);
                    Return.Merge(Result);
                }
                #endregion
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }
        public DataSet Refresh(DataRow dwInput)
        {
            DataSet SResult = new DataSet();
            DataSet STemp = new DataSet();
            DataSet SReturn = new DataSet();
            try
            {
                INTEXDBC.InsertICC_ABPOS_MCO(null, 2, "");
                INTEXDBC.UpdateICC_ABPOS_MCO(null, 2, "");
                INTEXDBC.UpdateICC_ABPOS_MCO(null, 3, "");
                INTEXDBC.UpdateICC_ABPOS_MCO(null, 4, "");
                INTEXDBC.UpdateICC_ABPOS_MCO(null, 5, "");
                INTEXDBC.UpdateICC_ABPOS_MCO(null, 6, "");
                INTEXDBC.UpdateICC_ABPOS_MCO(null, 7, "");
                INTEXDBC.UpdateICC_ABPOS_MCO(null, 8, "");
                INTEXDBC.UpdateICC_ABPOS_MCO(null, 9, "");
                INTEXDBC.UpdateICC_ABPOS_MCO(null, 10, "");
                INTEXDBC.UpdateICC_ABPOS_MCO(null, 11, "");
                INTEXDBC.UpdateICC_ABPOS_MCO(null, 12, "");
                INTEXDBC.UpdateICC_ABPOS_MCO(null, 9999, "");
                INTEXDBC.UpdateICC_ABAUFM(null, 22);//面料的标准价格
                INTEXDBC.UpdateICC_ABAUFM(null, 27);//面料颜色的标准价格
                INTEXDBC.UpdateICC_ABAUFM(null, 23);
                INTEXDBC.UpdateICC_ABAUFM(null, 41);
                INTEXDBC.UpdateICC_ABAUFM(null, 42);
                INTEXDBC.UpdateICC_ABAUFM(null, 51);
                INTEXDBC.UpdateICC_ABAUFM(null, 52);
                INTEXDBC.UpdateICC_ABAUFM(null, 53);
                INTEXDBC.UpdateICC_ABAUFM(null, 54);
                //INTEXDBC.UpdateICC_ABAUFM(null, 21);
                SResult = INTEXDBC.QueryICC_ABPOS_MCO(dwInput, 2); 
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }

        public DataSet ResetData(SETINTEXDB src, DataRow dwABCO)
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();

            try
            {
                //按颜色的成本分析

                SReturn.Merge(src.ABPOS);
                SReturn.Merge(ResetDECK(src));
                SReturn.Merge(src.ICC_ABAUFM);
                SReturn.Merge(src.ABKOPF);
                #region======Update ABCO================
                string filter = "KRS='" + dwABCO["KRS"].ToString() + "' AND LFD='" + dwABCO["LFD"].ToString() + "'";
                DataTable tb = src.ICC_ABPOS_MCO.Select(filter).CopyToDataTable();
                tb.TableName = "ICC_ABPOS_MCO";
                SReturn.Merge(tb);
                #endregion
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        //计算成本时，考虑需求数量是米，但是采购数量是公斤
        public DataSet ResetDECK(SETINTEXDB src)
        {
            DataSet Result = new DataSet();
            DataSet SReturn = new DataSet();
            double colortotal = 0;
            double total = 0;
            int yc = 0;
            try
            {
                DataTable tbFabric = INTEXDB.ICC_WDECKMAT.Clone();
                tbFabric.TableName = "ICC_WDECKMAT";
                DataTable tbFMat = INTEXDB.ICC_FDECKMAT.Clone();
                tbFMat.TableName = "ICC_FDECKMAT";
                DataRow row = null;
                foreach (DataRow RX in src.ICC_ABAUFM.Rows)
                {
                    if (RX["CONTROL"].ToString() == "Y")
                        continue;
                    if (RX["MTYPE"].ToString() == "T")
                        continue;
                    decimal RT = 1;
                    if (RX["WAUEBM"].ToString().Trim() == "KG")
                    {
                        RX["CONTROL"] = "Y";
                        RT = Convert.ToDecimal(RX["WAFGRLFD"].ToString()) * Convert.ToDecimal(0.001);
                        decimal QTYSTD = System.Convert.ToDecimal(RX["QTYSTD"].ToString());
                        RX["QTYSTD"] =Math.Round( QTYSTD * RT,1);
                        decimal QTYPLAN = System.Convert.ToDecimal(RX["QTYPLAN"].ToString());
                        RX["QTYPLAN"] = Math.Round(QTYPLAN * RT,1);
                        decimal QTYACT = System.Convert.ToDecimal(RX["QTYACT"].ToString());
                        RX["QTYACT"] = Math.Round(QTYACT * RT,1);
                    }
                }
                var qxd = from XD in src.ICC_ABAUFM.AsEnumerable()
                          group XD by new
                          {
                              MTYPE = XD["MTYPE"].ToString(),
                              ART = XD["ABAART"].ToString(),
                              VAR = XD["ABAVAR"].ToString(),
                              FARBE = XD["ABAFARBE"].ToString(),
                              ASIZE = XD["ABAGROE"].ToString(),

                          }
                              into g
                              select new
                              {
                                  KEY = g.Key,
                                  UM = g.Max(XD => XD.Field<string>("WAUEBM")),
                                  FGRLFD = g.Max(XD => XD.Field<Decimal>("WAFGRLFD")),
                                  TQTY = g.Sum(XD => Convert.ToDecimal(XD.Field<Decimal>("ABARESMG"))),
                                  QTYACT = g.Sum(XD => Convert.ToDecimal(XD.Field<Decimal>("QTYACT"))),
                              };
                foreach (var XD in qxd)
                {
                    switch (XD.KEY.MTYPE.Trim())
                    {
                        case "T":
                            row = tbFMat.NewRow();
                            row["ART"] = XD.KEY.ART;
                            row["VAR"] = XD.KEY.VAR;
                            row["FARBE"] = XD.KEY.FARBE;
                            row["SIZE"] = XD.KEY.ASIZE;
                            row["UM"] = XD.UM;
                            row["TQTY"] = XD.TQTY;
                            tbFMat.Rows.Add(row);
                            break;
                        case "F":
                            row = tbFabric.NewRow();
                            row["ART"] = XD.KEY.ART;
                            row["VAR"] = XD.KEY.VAR;
                            row["FARBE"] = XD.KEY.FARBE;
                            row["UM"] = XD.UM;
                            row["FGRLFD"] = XD.FGRLFD;
                            row["TQTY"] = XD.TQTY;
                            row["STQTY"] = XD.QTYACT;
                            tbFabric.Rows.Add(row);
                            break;
                    }
                }
                SReturn.Merge(tbFMat);
                SReturn.Merge(tbFabric);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
    }
}
